package com.qf.commons.web.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * author Ken
 * create_time 2022/12/3
 */
@Slf4j
public class FeignHeaderInterecptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        log.debug("[feign interceptor] 拦截器触发！！！！！");

        //获取当前请求的请求头，设置到template中
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes != null) {
            HttpServletRequest request = attributes.getRequest();

            String userJson = request.getHeader("userJson");
            if (userJson != null) {
                //设置个template 搞定
                template.header("userJson", userJson);
            }
        }
    }
}
